/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 添加仓库界面
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TStore;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.IDNameBean;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.service.TEmployeeService;
import com.ruipengkj.service.TStoreService;


public class DataStorageAddOrModifyDialog extends IDialog {

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent number;
	private JLabelJTextFieldComponent name;
	private JLabelJTextFieldComponent location;
	private JLabelJComboBoxComponent manager;//管理员
	private JLabelJTextFieldComponent priority;
	private JLabelJTextAreaComponent note;
	
	private JButton sureButton;
	private JButton cancelButton;
	
	private TStore tStore = null;
	private TStoreService storeService;
	private TEmployeeService tEmployeeService;//
	
	public boolean isSuccess=false;//判断是否添加成功
	public DataStorageAddOrModifyDialog(JDialog jf,String title,boolean b) {
		super(jf, title, b);
		storeService=BuildSpringApplication.getInstance().getTStoreServiceImpl();
		tEmployeeService=BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		this.setSize(350, 400);
		this.init();
		this.initListener();
		this.setLocationRelativeTo(null);
	}
	
	public void init() {
		JPanel main_jp = new BorderJPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		number= new JLabelJTextFieldComponent("  *", "仓库编号：", 30);
		name = new JLabelJTextFieldComponent("  *", "仓库名称：", 30);
		location = new JLabelJTextFieldComponent("  *", "仓库地址：", 30);
		manager = new JLabelJComboBoxComponent("*", "仓库管理员：",new String[]{},188);
		manager.addElement(new IDNameBean("0", "请选择"));
		priority= new JLabelJTextFieldComponent("*", "仓库优先级：","（数字越大，越在前面）", 15);
		note = new JLabelJTextAreaComponent("    备    注：", 23,3);
		
		number.getText().setText(UUID.GenUUID15());
		number.getText().setEditable(false);
		priority.getText().setText("0");
		
		main_jp.add(number);
		main_jp.add(name);
		main_jp.add(location);
		main_jp.add(manager);
		main_jp.add(priority);
		main_jp.add(note);
		
		JPanel p1 = new JPanel();
		sureButton=new RPJButtonWhite("保存(Ctrl+S)");
		cancelButton=new RPJButtonWhite("取消(Ctrl+Q)");
		p1.add(sureButton);
		p1.add(cancelButton);
		p1.setOpaque(false);
		main_jp.add(p1);
		this.add(main_jp);
		main_jp.setCursor(Util.CURSOR_DEFAULT);
	}

	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			String storeIDStr=number.getText().getText().trim();
			String nameStr=name.getText().getText().trim();
			String locationStr=location.getText().getText().trim();
			Object managerObj=manager.getBox().getSelectedItem();
			String priorityStr=priority.getText().getText().trim();
			String noteStr=note.getArea().getText().trim();
			if(nameStr.equals("")||nameStr.length()>128){
				JOptionPane.showMessageDialog(null, "仓库名称不能为空，且不能大于128个字");
				name.getText().requestFocus();
				return;
			}
			if(locationStr.equals("")||locationStr.length()>100){
				JOptionPane.showMessageDialog(null, "仓库地址不能为空，且不能大于100个字");
				location.getText().requestFocus();
				return;
			}
			if(noteStr.length()>100){
				JOptionPane.showMessageDialog(null, "备注不能大于100个字");
				note.getArea().requestFocus();
				return;
			}
			if(priorityStr.equals("")||!MatcherFormatUtil.MatcherNumberScheme(priorityStr)||priorityStr.length()>10){
				JOptionPane.showMessageDialog(null, "优先级只能输入数字，且不能大于10位");
				priority.getText().requestFocus();
				return;
			}
			IDNameBean bean=(IDNameBean)managerObj;
			if(bean.getStrId().equals("0")){
				JOptionPane.showMessageDialog(null, "请先选择仓库管理员");
				manager.getBox().requestFocus();
				return;
			}
			
			boolean isOpLog=false;
			if(tStore==null){
				tStore = new TStore();
				isOpLog=true;
			}
			tStore.setStoreID(storeIDStr);
			tStore.setName(nameStr);
			tStore.setLocation(locationStr);
			tStore.setManager(bean.getStrId());
			tStore.setPriority(Integer.parseInt(priorityStr));
			tStore.setNote(noteStr);
			try {
				if (isOpLog) {
					List<TStore> list = storeService.findByNameService(nameStr);
					if (list != null && list.size() != 0) {
						if (Util.showConfirmDialog("存在相同名字的仓库，确定添加吗？") != 0)
							return;
					}
				}
				this.storeService.saveOrUpdateService(tStore);
				this.isSuccess=true;
				if(isOpLog){
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加仓库信息");
				}else{
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改仓库信息");	
				}
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == cancelButton) {
			this.isSuccess=false;
			this.dispose();
		}
		super.actionPerformed(e);
	}
	
	public void showDialog(TStore tStore){
		this.isSuccess=false;
		SwingUtilities.invokeLater(queryEmp);
		if(tStore!=null){
			this.tStore=tStore;
			number.getText().setText(tStore.getStoreID());
			name .getText().setText(tStore.getName());
			location.getText().setText(tStore.getLocation());
//			manager.getText().setText(tStore.getManager());
			priority.getText().setText(tStore.getPriority()+"");
			note.getArea().setText(tStore.getNote());
		}
		this.setVisible(true);
	}

	public Runnable queryEmp=new Runnable() {
		@Override
		public void run() {
			try {
				List<TEmployee> list=tEmployeeService.findService(null);
				if(list!=null&&list.size()!=0){
					for(int i=0;i<list.size();i++){
						TEmployee emp=list.get(i);
						manager.addElement(new IDNameBean(emp.getEmployeeID(), emp.getName()));
						if(tStore!=null){
							if(emp.getEmployeeID().equals(tStore.getManager())){
								manager.getBox().setSelectedIndex(i+1);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	public static void main(String[] args) {
		DataStorageAddOrModifyDialog dialog = new DataStorageAddOrModifyDialog(null, "添加员工", true);
		dialog.setVisible(true);
	}
}
